package com.zlx.taskcenter.config;

import com.zlx.taskcenter.annotations.Consumer;
import com.zlx.taskcenter.common.RedisConsumerContainer;
import com.zlx.taskcenter.common.RedisQueueConfiguration;
import com.zlx.taskcenter.consumer.Impl.Detection;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;


/**
 * 消息队列初始化
 * 1、消息顺序性(List队列模式)
 * 2、消息ID唯一性(防止消息重复消费)
 * 3、消息一致性(提取消息时备份消息)
 */
@Configuration
@DependsOn(value = "redisTemplate")
public class RedisQueueConfig {

    // 定期检测消费者数量
    @Value("${redisQueue.detection.consumerNum:4}")
    private int detection_consumerNum;

    // 归档检测消费者数量
    @Value("${redisQueue.archiveDetection.consumerNum:2}")
    private int archiveDetection_consumerNum;

    @Value("${redisQueue.archivePackage.consumerNum:2}")
    private int archive_package_num;

    /**
     * 定时检测消费者
     */
    @Bean(destroyMethod = "beforeDestroy")
    public RedisConsumerContainer Detection() {
        RedisConsumerContainer config = new RedisConsumerContainer();
        // 指定消费者类
        Consumer annotation = Detection.class.getAnnotation(Consumer.class);
        String queue = annotation.queue().name();
        config.addConsumer(
                RedisQueueConfiguration.builder()
                        .queue(queue)
                        .consumerNum(detection_consumerNum)
                        .consumer(new Detection())
                        .build());
        config.init(queue);
        return config;
    }
}
